package com.cyandpos.pad.presentation;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.utils.MoneyUtils;
import com.squareup.picasso.Picasso;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class AdPresentation extends Presentation {

     private DecoratorViewPager decoratorViewPager;
     private DecoratorViewPager decoratorViewPager_two;
     private LinearLayout ll;
     private TextView tv_collect_money;
     private TextView tv_change_money;
     private TextView tv_no_vip;
     private LinearLayout ll_vip;
     private TextView tv_vip_name;
     private TextView tv_vip_type;
     private TextView tv_vip_cent;
     private ListView lv_goods;
     private TextView tv_goods_sums;
     private TextView tv_good_price;
     private TextView tv_discount_price;
     private TextView tv_pay_money;
     private ImageView iv_code;
     private ListView lv_pay;
     private List<View> list;
    private List<View> listTwo;
     private List<String> dataList;
    private List<Payment> paymentList;
     private Context context;
     private PresentationPagerAdapter presentationPagerAdapter;
    private PresentationPagerAdapter presentationPagerAdapterTwo;
     private int dex;
     private int dexTwo;
     private AdPresentationGoodAdapter adPresentationGoodAdapter;
     private List<Goods> goodsList;
     private int mode=0;
    public AdPresentation(Context outerContext, Display display) {
        super(outerContext, display);
        this.context=outerContext;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_presentation);
        init();
        initViewPager();
    }

    public void init(){
        decoratorViewPager=findViewById(R.id.decoratorViewPager);
        decoratorViewPager_two=findViewById(R.id.decoratorViewPager_two);
        ll=findViewById(R.id.ll);
        lv_pay=findViewById(R.id.lv_pay);
        tv_collect_money=findViewById(R.id.tv_collect_money);
        tv_change_money=findViewById(R.id.tv_change_money);
        tv_no_vip=findViewById(R.id.tv_no_vip);
        ll_vip=findViewById(R.id.ll_vip);
        tv_vip_name=findViewById(R.id.tv_vip_name);
        tv_vip_type=findViewById(R.id.tv_vip_type);
        tv_vip_cent=findViewById(R.id.tv_vip_cent);
        lv_goods=findViewById(R.id.lv_goods);
        tv_goods_sums=findViewById(R.id.tv_goods_sums);
        tv_good_price=findViewById(R.id.tv_good_price);
        tv_discount_price=findViewById(R.id.tv_discount_price);
        tv_pay_money=findViewById(R.id.tv_pay_money);
        iv_code=findViewById(R.id.iv_code);
    }

    public void initViewPager(){
        goodsList=new ArrayList<>();
        paymentList=new ArrayList<>();
        adPresentationGoodAdapter=new AdPresentationGoodAdapter(context,goodsList);
        lv_goods.setAdapter(adPresentationGoodAdapter);
        Picasso.get().load("https://img.tukuppt.com//png_preview/00/08/56/69BoZX9vFn.jpg!/fw/780").into(iv_code);
        list=new ArrayList<>();
        listTwo=new ArrayList<>();
        dataList=new ArrayList<>();
        if(!"".equals(ErpCommonData.imageData)&&ErpCommonData.imageData.contains(";")){
            String[] images=ErpCommonData.imageData.split(";");
            for(int i=0;i<images.length;i++){
                dataList.add(images[i]);
            }
        }
        for(int i=0;i<dataList.size();i++){
            View view= LayoutInflater.from(context).inflate(R.layout.item_image_viewpager,null,false);
            ImageView imageView=view.findViewById(R.id.image);
            Picasso.get().load(dataList.get(i)).into(imageView);
            list.add(view);
        }
        for(int i=0;i<dataList.size();i++){
            View view= LayoutInflater.from(context).inflate(R.layout.item_image_viewpager,null,false);
            ImageView imageView=view.findViewById(R.id.image);
            Picasso.get().load(dataList.get(i)).into(imageView);
            listTwo.add(view);
        }
        presentationPagerAdapter=new PresentationPagerAdapter(list);
        decoratorViewPager.setAdapter(presentationPagerAdapter);
        decoratorViewPager.startPager();
        decoratorViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                //初始化小圆点的背景
                dex = position;
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    if (dex == decoratorViewPager.getAdapter().getCount() - 1) {
                        decoratorViewPager.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                decoratorViewPager.setCurrentItem(0, false);
                            }
                        },Integer.parseInt(ErpCommonData.imageTime)*1000);
                    }
                }

            }
        });
        presentationPagerAdapterTwo=new PresentationPagerAdapter(listTwo);
        decoratorViewPager_two.setAdapter(presentationPagerAdapterTwo);
        decoratorViewPager_two.startPager();
        decoratorViewPager_two.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                //初始化小圆点的背景
                dexTwo = position;
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    if (dexTwo == decoratorViewPager_two.getAdapter().getCount() - 1) {
                        decoratorViewPager_two.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                decoratorViewPager_two.setCurrentItem(0, false);
                            }
                        },Integer.parseInt(ErpCommonData.imageTime)*1000);
                    }
                }

            }
        });
    }

    public void destory(){
        decoratorViewPager.stopPager();
    }

    public void destoryTwo(){
        decoratorViewPager_two.stopPager();
    }

    public void addMember(Member member){
        if (member != null) {
            ll_vip.setVisibility(View.VISIBLE);
            tv_no_vip.setVisibility(View.GONE);
            tv_vip_name.setText(member.getMemberName());
            tv_vip_type.setText(member.getMemberCardTypeName());
            tv_vip_cent.setText(member.getMemberCardValidCent());
        } else {
            ll_vip.setVisibility(View.GONE);
            tv_no_vip.setVisibility(View.VISIBLE);
        }
    }

    public void CalculatorPayed(TicketInfo mTicket) {
        tv_good_price.setText("￥ "+new DecimalFormat("0.00").format(new BigDecimal(mTicket.getTotalAmount()+ "").add(new BigDecimal(mTicket.getTotalDiscount())).doubleValue()));
        tv_pay_money.setText("￥ "+ MoneyUtils.getMoneyStr(mTicket.getTotalMoney()));
        tv_discount_price.setText("￥ "+new DecimalFormat("0.00").format(new BigDecimal(mTicket.getTotalDiscount() + "").doubleValue()));
        tv_change_money.setText("￥ "+new DecimalFormat("0.00").format(new BigDecimal(mTicket.getChangeAmount() + "").doubleValue()));
        tv_collect_money.setText("￥ "+MoneyUtils.getMoneyStr(mTicket.getHasPayed()));
    }

   public void setMode(int mode){
       this.mode=mode;
      switch (mode){
          case 0:
              ll.setVisibility(View.GONE);
              decoratorViewPager.setVisibility(View.VISIBLE);
              break;
          case 1:
              ll.setVisibility(View.VISIBLE);
              decoratorViewPager.setVisibility(View.GONE);
              break;
      }
   }

    public void setGoodsList(List<Goods> goodsList) {
        this.goodsList = goodsList;
        BigDecimal bigDecimalPrice=new BigDecimal("0.00");
        BigDecimal bigDecimalDiscount=new BigDecimal("0.00");
        adPresentationGoodAdapter=new AdPresentationGoodAdapter(context,goodsList);
        lv_goods.setAdapter(adPresentationGoodAdapter);
        tv_goods_sums.setText(goodsList.size()+"件");
        for(int i=0;i<goodsList.size();i++){
            if(goodsList.get(i).getSaleMoney()!=null) {
                bigDecimalPrice = bigDecimalPrice.add(new BigDecimal(goodsList.get(i).getSaleMoney()));
            }
            if(goodsList.get(i).getDiscount()!=null) {
                bigDecimalDiscount = bigDecimalDiscount.add(new BigDecimal(goodsList.get(i).getDiscount()));
            }
        }
        tv_good_price.setText("￥ "+new DecimalFormat("0.00").format(bigDecimalPrice.doubleValue()));
        tv_discount_price.setText("￥ "+new DecimalFormat("0.00").format(bigDecimalDiscount.doubleValue()));
        tv_pay_money.setText("￥ "+new DecimalFormat("0.00").format(bigDecimalPrice.subtract(bigDecimalDiscount).doubleValue()));
        tv_change_money.setText("￥ 0.00");
        tv_collect_money.setText("￥ 0.00");
    }

    public int getMode() {
        return mode;
    }

    public void setData(TicketInfo mTicket){
        Member member=mTicket.getMemberInfo();
        if (member != null) {
            ll_vip.setVisibility(View.VISIBLE);
            tv_no_vip.setVisibility(View.GONE);
            tv_vip_name.setText(member.getMemberName());
            tv_vip_type.setText(member.getMemberCardTypeName());
            tv_vip_cent.setText(member.getMemberCardValidCent());
        } else {
            ll_vip.setVisibility(View.GONE);
            tv_no_vip.setVisibility(View.VISIBLE);
        }

        goodsList=new ArrayList<>();
        for (int i = 0; i < mTicket.getSubTickets().size(); i++) {
            goodsList.addAll(mTicket.getSubTickets().get(i).getGoodsList());
        }
        adPresentationGoodAdapter=new AdPresentationGoodAdapter(context,goodsList);
        lv_goods.setAdapter(adPresentationGoodAdapter);
        tv_goods_sums.setText(goodsList.size()+"件");
        tv_good_price.setText("￥ "+new DecimalFormat("0.00").format(new BigDecimal(mTicket.getTotalAmount()+ "").add(new BigDecimal(mTicket.getTotalDiscount())).doubleValue()));
        tv_pay_money.setText("￥ "+ MoneyUtils.getMoneyStr(mTicket.getTotalMoney()));
        tv_discount_price.setText("￥ "+new DecimalFormat("0.00").format(new BigDecimal(mTicket.getTotalDiscount() + "").doubleValue()));
        tv_change_money.setText("￥ "+new DecimalFormat("0.00").format(new BigDecimal(mTicket.getChangeAmount() + "").doubleValue()));
        tv_collect_money.setText("￥ "+MoneyUtils.getMoneyStr(mTicket.getHasPayed()));
    }

    public void setPayment(List<Payment> paymentList){
        this.paymentList=paymentList;
        AdPresentationPayAdapter adPresentationPayAdapter=new AdPresentationPayAdapter(context,paymentList);
        lv_pay.setAdapter(adPresentationPayAdapter);
    }
}

